let upload = require('../../common/upload')
Page({
    behaviors: [upload],
    data: {
        name: '',
        nameErr: false,
        introduction: '',
        address: '',
        addressErr: false,
        house: '',
        houseErr: false,
        concat: '',
        concatErr: false,
        tel: '',
        telErr: false,
        // picture: [], //供应商图片
        // pictureErr: false,
        license: [], //营业执照
        // licenseErr: false,
        front_ID: [], //身份证正面
        front_IDErr: false,
        reverse_ID: [], //身份证反面
        reverse_IDErr: false,
        pictureFormData: { from: 'comment' },
        licenseFormData: { from: 'comment' },
        front_IDFormData: { from: 'comment' },
        reverse_IDFormData: { from: 'comment' },
        region: [],
        province: '',
        province_code: '',
        city: '',
        city_code: '',
        district: '',
        county_code: '',
        buttonLoading: false,
        agreement: true, //同意协议
    },
    submit() {
        const { name, introduction, address, house, concat, tel, license, front_ID, reverse_ID, agreement } = this.data
        if (name === '') return this.setData({ nameErr: true })
        if (address === '') return this.setData({ addressErr: true })
        if (house === '') return this.setData({ houseErr: true })
        if (concat === '') return this.setData({ concatErr: true })
        if (tel === '') return this.setData({ telErr: true })
        if (tel.length !== 11) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none'
            })
            return this.setData({ telErr: true })
        }

        //手机号验证正则
        // const telReg = new RegExp("^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$")
        // if (!telReg.test(tel)) {
        //     this.setData({ telErr: true })
        //     return wx.showToast({
        //         title: '手机号码输入有误',
        //         icon: 'none'
        //     })
        // }
        // if (picture.length === 0) return this.setData({ pictureErr: true })
        // if (license.length === 0) return this.setData({ licenseErr: true })
        if (front_ID.length === 0) return this.setData({ front_IDErr: true })
        if (reverse_ID.length === 0) return this.setData({ reverse_IDErr: true })

        let flag = true
        let field = ['license', 'front_ID', 'reverse_ID']
        for (let list = 0; list < field.length; list++) {
            let data = this.data[field[list]]
            for (let i = 0; i < data.length; i++) {
                if (data[i].status !== 'done') {
                    flag = false
                    break;
                }
            }
        }
        if (!flag) return wx.showToast({
            title: '有图片正在上传中或者上传失败',
            icon: 'none',
            duration: 2000
        })

        if (!agreement) return wx.showToast({
            title: '请阅读并同意用户服务协议',
            icon: 'none',
        })
        this.setData({ buttonLoading: true })

        let reqData = {
            com_title: name,
            contact: concat,
            mobile: tel,
            addr: address,
            house: house,
            id_photo_front: front_ID[0].uploadPath,
            id_photo_back: reverse_ID[0].uploadPath,
        }
        if (introduction) reqData.intro = introduction
        if (license.length > 0) reqData.account_photo = license[0].uploadPath

        this.request({
            url: '/api/supplier/combine',
            method: 'POST',
            data: reqData
        }, () => {
            wx.$dialog.alert({
                title: '提示',
                message: '提交成功，请耐心等待审核',
                confirmButtonText: '我知道了',
                theme: 'round-button',
                beforeClose: (action) => new Promise((resolve) => {
                    resolve(true)
                    wx.navigateBack()
                })
            })
        }, () => {
            this.setData({ buttonLoading: false })
        })
    },
    //选择地区
    cityChange({ detail }) {
        const [province, city, district] = detail.value
        const [province_code, city_code, county_code] = detail.code
        this.setData({
            province,
            province_code,
            city,
            city_code,
            district,
            county_code,
            address: province + city + district,
            region: detail.value,
            addressErr: false
        })
    },
    clearErr() {
        this.setData({
            nameErr: false,
            addressErr: false,
            concatErr: false,
            telErr: false,
            // pictureErr: false,
            // licenseErr: false,
            front_IDErr: false,
            reverse_IDErr: false,
            houseErr: false
        })
    },
    //协议
    onAgreeChange({ detail }) {
        this.setData({ agreement: detail })
    }
})